package com.cis.web.controller.education;

import java.util.ArrayList;
import java.util.List;

import com.cis.common.utils.ShiroUtils;
import com.cis.education.domain.EduCourse;
import com.cis.education.domain.EduCourseStudent;
import com.cis.education.service.IEduCourseService;
import com.cis.education.service.IEduCourseStudentService;
import com.cis.framework.shiro.service.SysPasswordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cis.common.annotation.Log;
import com.cis.common.enums.BusinessType;
import com.cis.education.domain.EduStudent;
import com.cis.education.service.IEduStudentService;
import com.cis.common.core.controller.BaseController;
import com.cis.common.core.domain.AjaxResult;
import com.cis.common.utils.poi.ExcelUtil;
import com.cis.common.core.page.TableDataInfo;

/**
 * 学生档案管理Controller
 * 
 * @author 管理员
 * @date 2022-01-12
 */
@Controller
@RequestMapping("/education/student")
public class EduStudentController extends BaseController
{
    private String prefix = "education/student";

    @Autowired
    private IEduStudentService eduStudentService;

    @Autowired
    private IEduCourseService eduCourseService;

    @Autowired
    private IEduCourseStudentService eduCourseStudentService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("education:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生档案管理列表
     */
    @RequiresPermissions("education:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EduStudent eduStudent)
    {
        startPage();
        List<EduStudent> list = eduStudentService.selectEduStudentList(eduStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生档案管理列表
     */
    @RequiresPermissions("education:student:export")
    @Log(title = "学生档案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduStudent eduStudent)
    {
        List<EduStudent> list = eduStudentService.selectEduStudentList(eduStudent);
        ExcelUtil<EduStudent> util = new ExcelUtil<EduStudent>(EduStudent.class);
        return util.exportExcel(list, "学生档案管理数据");
    }

    /**
     * 新增学生档案管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<EduCourse> eduCourses = eduCourseService.selectEduCourseList(new EduCourse());
        mmap.put("eduCourses", eduCourses);
        return prefix + "/add";
    }

    /**
     * 新增保存学生档案管理
     */
    @RequiresPermissions("education:student:add")
    @Log(title = "学生档案管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EduStudent eduStudent)
    {
        //设置学生学号
        List<EduStudent> eduStudents = eduStudentService.selectEduStudentList(new EduStudent());
        int studentNum = eduStudents.size();
        if (eduStudents.isEmpty()){
            eduStudent.setId(creatId(eduStudent.getAcademy(), studentNum));
        }else{
            eduStudent.setId(extendId(eduStudents.get(studentNum-1).getId()));
        }
        eduStudent.setSalt(ShiroUtils.randomSalt());
        eduStudent.setPassword(passwordService.encryptPassword(
                eduStudent.getName(), eduStudent.getPassword(), eduStudent.getSalt()
        ));
        return toAjax(eduStudentService.insertEduStudent(eduStudent));
    }

    /**
     * 修改学生档案管理
     */
    @RequiresPermissions("education:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        EduStudent eduStudent = eduStudentService.selectEduStudentById(id);
        List<EduCourse> eduCourses = eduCourseService.selectEduCourseList(new EduCourse());

        //获取同学选课记录
        EduCourseStudent eduCourseStudent = new EduCourseStudent();
        eduCourseStudent.setStudentId(id);
        List<EduCourseStudent> eduCourseStudents = eduCourseStudentService.selectEduCourseStudentList(eduCourseStudent);

        //存入学生个人信息
        List<Integer> courseList = new ArrayList<>();
        for(EduCourseStudent ecs : eduCourseStudents){
            courseList.add(ecs.getCourseId());
        }
        eduStudent.setCourseList(courseList);

        mmap.put("eduStudent", eduStudent);
        mmap.put("eduCourses", eduCourses);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生档案管理
     */
    @RequiresPermissions("education:student:edit")
    @Log(title = "学生档案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduStudent eduStudent)
    {
        return toAjax(eduStudentService.updateEduStudent(eduStudent));
    }

    /**
     * 删除学生档案管理
     */
    @RequiresPermissions("education:student:remove")
    @Log(title = "学生档案管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eduStudentService.deleteEduStudentByIds(ids));
    }


    private String creatId(String academy, int studentNum) {
        return academy +
                String.format("%04d", studentNum);
    }

    private String extendId(String preStudentId) {
        String idStr = preStudentId.substring(3,7);
        int num = Integer.parseInt(idStr);
        String studentId = preStudentId.substring(0,3) + String.format("%04d", num);
        return studentId;
    }

}
